<?php

namespace app\admin\controller;
class Article extends base
{
    //前置方法
    protected $beforeActionList = [
        'map'  =>  ['only'=>'index'],
        'addclass' => ['only' => 'edit'],
    ];

    /**
     * 搜索
     */
    protected function map(){
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }

            if (!empty($data['comid'])) {
                $this->datas ['comid'] =$data['comid'];
            }

            if (!empty($data['contribute'])) {
                $this->datas ['contribute'] = 1;
            }else{
                $this->datas ['contribute'] = 0;
            }
            $this->datas['lang'] = cookie('think_var_admin');
        }
    }

    public function index()
    {
        $this->assign('en', $this->cheaklangnav('en-us'));
        $this->assign('zh', $this->cheaklangnav('zh-cn'));

        $order[$this->order] = $this->sort;
        $list = $this->model->page_index($this->datas, $order)->appends($this->request->param())->each(function($item, $key){
            $item['dangwei'] =  db('company')->where('id',$item['comid'])->value('title');
            $item['navtitle'] =  db('nav')->where('id',$item['cid'])->value('name');
            return $item;
        });
        $contribute = $this->request->param('contribute');
        if(!empty($contribute)){
            $dd = db('company')->column('id');
            foreach($dd as $key){
                $d['allnum']= db('article')->where('comid',$key)->where('contribute',1)->count();
                $d['passnum']=db('article')->where('comid',$key)->where('contribute',1)->where('status',1)->count();
                db('company')->where('id',$key)->update($d);
            }
        }

        $this->assign('num', $this->model->where($this->datas)->count());
        $this->assign('list', $list);
        $this->assign('companyclass',model('company')->select());
        $this->assign('nav', $this->cheaklangnav(cookie('think_var_admin')));
        $this->assign('addcid', $this->request->param('cid') ? $this->request->param('cid') : '');
        return $this->fetch();
    }

    /**
     * 栏目
     */
    protected function addclass()
    {
        $this->datas['lang'] = cookie('think_var_admin');
        $this->datas['status']='1';
        $company= model('company')->where($this->datas)->select();
        $wechatkey = model('Article')->where('id',  $this->request->param('id'))->value('wechatkey');
        $wechatkey = json_decode($wechatkey);
        $this->assign([
            'companyclass'=> $company,
            'wechatkey'=>$wechatkey,
        ]);
    }

    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $data = $this->request->post();
        $data['create_time'] =$data['create_time']?strtotime($data['create_time']):time();

        if(isset($data['source'],$data['review'])){
            $obj['source'] =  $data['source']?$data['source']:'';
            $obj['review'] =  $data['review']?$data['review']:'';
            $data['wechatkey'] = json_encode($obj);
        }

        $result = $this->validate($data, 'Article');
        if (true !== $result) {
            return ajax_info($result);
        }
        if(!empty($data['id'])){
            !empty($data['status'])?$data['status']:'0';
           return $this->update($data);
        }

        $data['lang']=cookie('think_var_admin');
        $data['status']='1';
        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }
    }
}
